package com.laikan.reader.bookfriend.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.BookDetailBean;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.PublicUtil;
import com.socks.library.KLog;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * 修改昵称页
 */
public class NickName extends BasicActivity implements OnClickListener {

    private ImageView back_tv;// back
    private EditText nickname;// 输入框
    private TextView submit;// submit
    private String userName;
    Drawable drawable_back_tv;
    private RelativeLayout back_layout;
    private LoadingDialog loadingDialog;

    @Override
    protected void assignViews() {
        loadingDialog = new LoadingDialog(mContext);
        setContentView(R.layout.lk_nickname);
        back_tv = (ImageView)findViewById(R.id.iv_back);
        nickname = (EditText)findViewById(R.id.et_nickname);
        submit = (TextView)findViewById(R.id.tv_submit);
        back_layout = (RelativeLayout)findViewById(R.id.back_layout);
    }

    @Override
    protected void initData() {
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        back_tv.setImageDrawable(drawable_back_tv);
    }

    @Override
    protected void setListener() {
        back_layout.setOnClickListener(this);
        submit.setOnClickListener(this);
        nickname.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
                if (!hasFocus) {
                    imm.hideSoftInputFromWindow(nickname.getWindowToken(), 0);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.back_layout:
                nickname.clearFocus();
                finish();
                break;
            case R.id.tv_submit:
                userName = nickname.getText().toString().trim();
                submit();
                break;
        }
    }

    private void submit() {
        loadingDialog.show();
        Map<String, String> map = new HashMap<String, String>();
        map.put("userName", userName);
        map.put("sd", SPUtil.getString("token", ""));
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                loadingDialog.dismiss();
                BaseDataBean<BookDetailBean> result;
                Gson gson = new Gson();
                Type type = new TypeToken<BaseDataBean<BookDetailBean>>() {
                }.getType();
                try {
                    result = gson.fromJson(httpResponse.responseContent, type);
                    if (Constants.RESULT_OK.equals(result.getResult())) {
                        if ("0".equals(result.getError_no())) {
                            ToastMgr.showShortToast(R.string.gender_modify_success);
                            Intent intent = new Intent();
                            intent.putExtra("nick", userName);
                            setResult(1, intent);
                            finish();
                        }else {
                            ToastMgr.showShortToast(result.getError_msg());
                        }
                    }else {
                        ToastMgr.showShortToast(result.getError_msg());
                    }
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                loadingDialog.dismiss();
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。30");
            }
            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL2() + "user/update_user_name");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL2() + "user/update_user_name", map, null, null, requestListener);
        // APIProtocol.getRootURL2()

    }

}
